#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        if (n == 0) return 0;

        vector<int> prefix(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            prefix[i] = prefix[i - 1] + nums[i - 1];
        }

        int minLen = n + 1;

        for (int i = 1; i <= n; ++i) {
            int toFind = target + prefix[i - 1];
            auto it = lower_bound(prefix.begin(), prefix.end(), toFind);
            if (it != prefix.end()) {
                int len = distance(prefix.begin() + i - 1, it);
                minLen = min(minLen, len);
            }
        }

        return minLen == n + 1 ? 0 : minLen;
    }
};